<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0);
    $fields=array('id','title','image','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_cmso2o_group',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'下架','1'=>'上架');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级分类管理'),
			array('op'=>'edit','title'=>'添加分类'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		),'action'=>array(
			array('type'=>'href','op'=>'list2','title'=>'二级菜单管理'),
		)),
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='list2'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0);
	$params['parentid']=intval($_RQ['id'])?:intval($_RQ['parentid']);
	$return['params']['parentid'] = $params['parentid'];
	$parenttitle=pdo_getcolumn('jy_cmso2o_group',array('id'=>$params['parentid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
    $fields=array('id','title','image','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_cmso2o_group',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'下架','1'=>'上架');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级分类管理'),
			array('op'=>'list2','title'=>$parenttitle.'分类列表'),
			array('op'=>'edit','title'=>'添加分类','query'=>'parentid='.$params['parentid']),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		),'action'=>array(
			array('type'=>'href','op'=>'list3','title'=>'三级菜单管理'),
		)),
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='list3'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0);
	$params['parentid']=intval($_RQ['id'])?:intval($_RQ['parentid']);
	$return['params']['parentid'] = $params['parentid'];
	$parenttitle=pdo_getcolumn('jy_cmso2o_group',array('id'=>$params['parentid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
	$parentid2=pdo_getcolumn('jy_cmso2o_group',array('id'=>$params['parentid']),'parentid');
	$parenttitle2=pdo_getcolumn('jy_cmso2o_group',array('id'=>$parentid2),'title');
	$parenttitle2=$parenttitle2?'【'.$parenttitle2.'】子':'';
    $fields=array('id','title','image','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_cmso2o_group',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'下架','1'=>'上架');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级分类管理'),
			array('op'=>'list2','title'=>$parenttitle2.'分类','query'=>'parentid='.$parentid2),
			array('op'=>'list3','title'=>$parenttitle.'分类'),
			array('op'=>'edit','title'=>'添加分类','query'=>'parentid='.$params['parentid']),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_cmso2o_group',array('id'=>$id));
	}else{
		$item=array();
		if($_RQ['parentid']){
			$item['parentid']=intval($_RQ['parentid']);
			$item['parenttitle']=pdo_getcolumn('jy_cmso2o_group',array('id'=>$item['parentid']),'title');
		}
	}
    $return['item']=$item;
	$return['status']=array('1'=>'上架','0'=>'下架');
	$return['is']=array('1'=>'是','0'=>'否');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回一级分类列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'desc','title'=>'详情','type'=>'text'),
			array('name'=>'ishome','title'=>'是否首页显示','type'=>'radio','options'=>'is'),	
			array('name'=>'ishot','title'=>'是否热门','type'=>'radio','options'=>'is'),	
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),	
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			)
	);
	if($_RQ['parentid']){
		array_unshift($htmls['edit'],array('name'=>'parenttitle','title'=>'上级分类','type'=>'string','read'=>true));
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写标题','error');
	}
	if(!$postdata['image']){
		exi('请上传图片','error');
	}
	$data=array_fields(array('title','image','desc','ishome','ishot','status','parentid','paixu'),$postdata);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_cmso2o_group',$data,array('id'=>$id));
		exi('设置成功！','','goback');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_cmso2o_group',$data);
		exi('添加成功！','','goback');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_cmso2o_group',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_cmso2o_group',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>